// pages/booking/booking.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectedLevel: '请选择', 
    levelOptions: ['国家一级心理咨询师', '国家二级心理咨询师', '国家三级心理咨询师'],
    selectedCategory: '请选择',
    activeIndex: 3, 
    categoryOptions: [
      "家庭关系", "亲自关系", "婚恋感情", "生活问题",
      "个体咨询", "人际关系", "压力情绪", "儿童心理",
      "青少年", "成年人", "职场问题", "心理危机干预",
      "精神疾病治疗", "抑郁症", "老年心理", "忧郁症"
    ],
    showCategorySheet: false, // 控制弹窗显示隐藏
    showLevelSheet: false, // 控制弹层显示隐藏
    activeIndex: 0,// 当前选中的索引
    isShowPicker: false, // 控制弹窗显示
    showPicker: false,
    showDatePicker: false,
    isOpen: false,
    isShowTimePicker: false,
    isTimePickerShow: false,
    datePickerValue: [2019 - 2000, 4, 20],
    yearRange: [],
    monthRange: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
    dayRange: [], 
    timePickerValue: [9, 30],
    hourRange: [],
    minuteRange: [],
    selectedTime: '请选择'
  },

  toggleLevelSheet(e) {
    app.globalData.level=e.detail.selectedLevel
    this.setData({
      showLevelSheet: !this.data.showLevelSheet,
    });
  },
  chooseLevel(e) {
    const index = e.currentTarget.dataset.index;
    app.globalData.level=this.data.levelOptions[index]
    this.setData({
      selectedLevel: this.data.levelOptions[index],
      activeIndex: index,
      showLevelSheet: false 
    });
  },
  toggleCategorySheet() {
    this.setData({
      showCategorySheet: !this.data.showCategorySheet
    });
  },
  ToIndex(){
    wx.switchTab({
      url: '/pages/index/index',
      success: function() {
        console.log('取消成功')
      },
    })
  },

  chooseCategory(e) {
    const index = e.currentTarget.dataset.index;
    app.globalData.sort=this.data.categoryOptions[index]
    this.setData({
      selectedCategory: this.data.categoryOptions[index],
      activeIndex: index,
      showCategorySheet: false
    });
  },
  switchTab(e) {
    this.setData({ activeTab: e.currentTarget.dataset.tab });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const yearArr = [];
    for (let i = 2000; i <= 2030; i++) {
      yearArr.push(i + '年');
    }
    this.setData({
      yearRange: yearArr
    });
    // 初始化小时、分钟范围
    const hourArr = [];
    const minuteArr = [];
    for (let h = 0; h < 24; h++) {
      hourArr.push(h + '时');
    }
    for (let m = 0; m < 60; m++) {
      minuteArr.push(m + '分');
    }
    this.setData({
      hourRange: hourArr,
      minuteRange: minuteArr
    });
    this.updateDayRange(2019, 5);
  },
  showTimePicker() {
    this.setData({
      isTimePickerShow: true
    });
  },

  onDateChange(e) {
    const val = e.detail.value;
    const year = 2000 + val[0];
    const month = val[1] + 1;
    this.setData({
      datePickerValue: val
    });
    this.updateDayRange(year, month);
  },

  onTimeChange(e) {
    this.setData({
      timePickerValue: e.detail.value
    });
  },

  confirmTime() {
    const year = 2000 + this.data.datePickerValue[0];
    const month = this.data.datePickerValue[1] + 1;
    const day = this.data.datePickerValue[2] + 1; 
    const hour = this.data.timePickerValue[0];
    const minute = this.data.timePickerValue[1];
    const formatTime = `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day} ${hour < 10 ? '0' + hour : hour}:${minute < 10 ? '0' + minute : minute}`;
    this.setData({
      selectedTime: formatTime,
      isTimePickerShow: false
    });
    const app = getApp();
    app.globalData.selectedTimeGlobal = formatTime;
  },
  cancelTime() {
    this.setData({
      isTimePickerShow: false
    });
  },
  updateDayRange(year, month) {
    let dayCount = 31;
    if (month === 4 || month === 6 || month === 9 || month === 11) {
      dayCount = 30;
    } else if (month === 2) {
      dayCount = (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0) ? 29 : 28;
    }
    const dayArr = [];
    for (let d = 1; d <= dayCount; d++) {
      dayArr.push(d + '日');
    }
    this.setData({
      dayRange: dayArr
    });
  },
  Tobooksing() {
    const params = {
      selectedCategory: this.data.selectedCategory,
      selectedLevel: this.data.selectedLevel
    };
    wx.navigateTo({
       url: '/pages/index/booking/booksing/booksing'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})